﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DTO;
using System.IO;
using BUS;
using System.Data.SqlClient;
namespace GPBenhLanh
{
    public partial class frmInHangLoat : DevExpress.XtraEditors.XtraForm
    {
        public frmInHangLoat()
        {
            InitializeComponent();
        }
        public static string _strConnectPK;
        SqlConnection conpk;

        /// <summary>
        /// get thong tin ket noi den database phKham
        /// </summary>
        public void getConnectStringPK()
        {
            string _sConnection = Uit.it_Conn.getConnectString("ConnectPhkham");
            _strConnectPK = axEnCrypt1.DeCrypt(_sConnection, Uit.it_Conn.getPasswordString());
        }

        public void CnPKham()
        {
            if (conpk == null || conpk.State == ConnectionState.Closed)
            {
                getConnectStringPK();
                conpk = new SqlConnection();
                conpk = Uit.it_Conn.getConnect(_strConnectPK);
            }
        }

        private void frmInHangLoat_Load(object sender, EventArgs e)
        {
            radLoaiKQ.EditValue = 1;
            radLoaiKQ.SelectedIndex = 0;
            txtNam.Text = DateTime.Now.Year.ToString("0000");
            CnPKham();
            DataTable dtPPNhuom = new DataTable();
            dtPPNhuom = GetThongTinSearchBUS.GetDSPPNhuom();
            comPPNhuom.Properties.DataSource = dtPPNhuom;
            comPPNhuom.Properties.DisplayMember = "TenPPNhuom";
            comPPNhuom.Properties.ValueMember = "IDPPNhuom";
            // Specify the separator character.
            comPPNhuom.Properties.SeparatorChar = ';';
            //setdefaut value pp Nhuom is H&E
            comPPNhuom.SetEditValue(1);
            // Disable the H&E item.
            comPPNhuom.Properties.Items[0].Enabled = false;   
        }

        string CreateSTB(int loai, int STB, string YY,string Prefix)
        {
           
            string SoTB = "";
            if (loai == 1)
            {
                SoTB = Prefix + YY + "-" + STB.ToString("0000");               
            }
            else
            {
                SoTB = Prefix + YY + "-" + STB.ToString("00000");
            }
            return SoTB;
        }

        private void btnInKQ_Click(object sender, EventArgs e)
        {
            int LoaiKQ = Convert.ToInt32(radLoaiKQ.EditValue);
            int iTuSTB = Convert.ToInt32(txtSoTieuBanTu.Text);
            int iDenSTB = Convert.ToInt32(txtSoTieuBanDen.Text);

            string YY = txtNam.Text.Substring(2,2);
            if (iTuSTB == iDenSTB && iTuSTB ==0)
                return;
            string curSoTB = "";
            //swap
            if (iTuSTB > iDenSTB)
            {
                int tepm = iTuSTB;
                iTuSTB = iDenSTB;
                iDenSTB = tepm;
                txtSoTieuBanTu.Text = iTuSTB.ToString();
                txtSoTieuBanDen.Text = iDenSTB.ToString();
            }

            switch (LoaiKQ)
            {
                case 1://in cắt lạnh                    
                    for (int i = iTuSTB; i <= iDenSTB; i++)
                    {                        
                        curSoTB = CreateSTB(1, i, YY, hensu.TienToCL);
                        rptPhieuKQ rptKQ = new rptPhieuKQ();
                        int rsGPB = SerDataToReport_CL(rptKQ, curSoTB, 1);
                        if (rsGPB == 0)
                            rptKQ.Print();
                    }                   
                    break;
                case 2://in kq gpb
                    for (int i = iTuSTB; i <= iDenSTB; i++)
                    {
                        curSoTB = CreateSTB(2, i, YY, hensu.TienToGPB);
                        rptPhieuKQ rptKQ = new rptPhieuKQ();
                        int rsGPB = SerDataToReport_GPB(rptKQ, curSoTB, 2);
                                             
                        if (rsGPB == 0)
                            rptKQ.Print();
                    }
                    break;
                case 3://in kết quả fna
                    for (int i = iTuSTB; i <= iDenSTB; i++)
                    {
                        curSoTB = CreateSTB(2, i, YY, hensu.TienToFNA);
                        rptKQFNA rptKQ = new rptKQFNA();
                        int rsFNA = SerDataToReport_FNA(rptKQ, curSoTB, 3);
                        if (rsFNA == 0)
                            rptKQ.Print();
                    }
                    break;
                case 4://in kết quả pap
                    for (int i = iTuSTB; i <= iDenSTB; i++)
                    {
                        curSoTB = CreateSTB(2, i, YY, hensu.TienToPAP);
                        rptKQPAP rptKQ = new rptKQPAP();
                        int rsPAP = SerDataToReport_PAP(rptKQ, curSoTB);
                        if (rsPAP == 0)
                            rptKQ.Print();
                    }
                    break;
            } 
        }

        public int SerDataToReport_CL(rptPhieuKQ rptKQ, string STB, int lan)
        {  
            DateTime cdate = DateTime.Today;
            DataTable dt = KQBUS.GetKetQua(STB, lan);

            if (dt.Rows.Count > 0)
            {
                rptKQ.txtSoTieuBan.Text = dt.Rows[0]["SoTieuBan"].ToString();
                rptKQ.txtDiaChi.Text = dt.Rows[0]["DiaChi"].ToString();
                rptKQ.txtdaithe.Text = dt.Rows[0]["KQDaiThe"].ToString();
                rptKQ.txtVithe.Text = dt.Rows[0]["KQViThe"].ToString();
                if (dt.Rows[0]["KetLuan"].ToString() != "")
                {
                    rptKQ.txtketluan.Text = dt.Rows[0]["KetLuan"].ToString().ToUpper();
                }
                else
                {
                    rptKQ.lblketluan.Visible = false;
                }
                rptKQ.txtchatBP.Text = Uit.it_Font.VNIToUnicode(dt.Rows[0]["ChatBP"].ToString());
            }
            else
            {
                return 1;
            }
            string url_1 = "";
            string url_2 = "";
            string gioi = "";

            if (Convert.ToBoolean(dt.Rows[0]["GioiTinh"]))
                gioi = "Nam";
            else
                gioi = "Nữ";

            rptKQ.barSoHS.Text = dt.Rows[0]["SoHS"].ToString();
            rptKQ.txtSoBN.Text = dt.Rows[0]["SoBN"].ToString();
            rptKQ.txtTraiBenh.Text = dt.Rows[0]["TenDViChiDinh"].ToString();

            rptKQ.txthoten.Text = dt.Rows[0]["HoTen"].ToString();
            rptKQ.txtGioiTinh.Text = gioi;
            rptKQ.txttuoi.Text = dt.Rows[0]["NamSinh"].ToString()
                                + "(" + dt.Rows[0]["Tuoi"].ToString() + " Tuổi)";
            rptKQ.txtnghe.Text = dt.Rows[0]["Nghe"].ToString();
            rptKQ.txtchuandoan.Text = dt.Rows[0]["ChuanDoan"].ToString();

            DateTime ngaylm = new DateTime();
            ngaylm = Convert.ToDateTime(dt.Rows[0]["NgayLayMau"].ToString());
            rptKQ.txtngaylaymau.Text = ngaylm.ToString("dd/MM/yyyy");

            rptKQ.txtptv.Text = dt.Rows[0]["PTVien"].ToString();
            rptKQ.txtppmo.Text = Uit.it_Font.Vni2Unicode(dt.Rows[0]["PPMo"],false);

            DateTime ngaynhan = new DateTime();
            ngaynhan = Convert.ToDateTime(dt.Rows[0]["NgayNhan"].ToString());
            rptKQ.txtSoLam.Text = ngaynhan.ToString("dd/MM/yyyy, hh:mm"); //Xn.NgayNhan.ToString("dd/MM/yyyy, hh:mm");

            if ( dt.Rows[0]["KQViThe"].ToString() == "")
            {
                return 1;
            }
            
            url_1 = hensu.url + ngaynhan.Year + "\\"
                        + ngaynhan.Year.ToString().Substring(2, 2) + ngaynhan.Month.ToString("00")
                        + "\\x100_" + STB + ".jpg";

            url_2 = hensu.url + txtNam.Text + "\\"
                        + ngaynhan.Year.ToString().Substring(2, 2) + ngaynhan.Month.ToString("00")
                        + "\\x400_" + STB + ".jpg";

            if (File.Exists(url_1))
                rptKQ.piclan1.Image = new Bitmap(url_1);
            else
                rptKQ.piclan1.Visible = false;

            if (File.Exists(url_2))
                rptKQ.piclan2.Image = new Bitmap(url_2);
            else
                rptKQ.piclan2.Visible = false;

            
            cdate = DateTime.Parse(dt.Rows[0]["CDate"].ToString());

            rptKQ.txtketqualan.Text = "KẾT QUẢ CẮT LẠNH";
            rptKQ.txtngaythang.Text = "Ngày " + cdate.Day.ToString("00") +
                              " Tháng " + cdate.Month.ToString("00") +
                              " Năm " + cdate.Year.ToString("0000");

            if (rptKQ.piclan1.Image == null)
            {
                rptKQ.piclan1.Visible = false;
            }
            if (rptKQ.piclan2.Image == null)
            {
                rptKQ.piclan2.Visible = false;
            }
            rptKQ.txtngaygioin.Text = DateTime.Now.ToString("dd/MM/yyyy, hh:mm:ss");
            rptKQ.txtbsthuchien.Text = dt.Rows[0]["HoTenBSTH"].ToString(); //comBacSi.Text;
            //print
            
            return 0;
        }

        public int SerDataToReport_GPB(rptPhieuKQ rptKQ, string STB, int lan)
        {
            DateTime cdate = DateTime.Today;
            DataTable dt = KQBUS.GetKetQua(STB, lan);
            string url_1 = "";
            string url_2 = "";
            string gioi = "";

            if (dt.Rows.Count > 0)
            {

                rptKQ.txtSoTieuBan.Text = dt.Rows[0]["SoTieuBan"].ToString();
                rptKQ.txtDiaChi.Text = dt.Rows[0]["DiaChi"].ToString();
                string dait, kl;
                dait = dt.Rows[0]["KQDaiThe"].ToString();
                if (dait == "NNull")
                    dait = "";

                rptKQ.txtdaithe.Text = dait;
                rptKQ.txtVithe.Text = dt.Rows[0]["KQViThe"].ToString();
                string skl = dt.Rows[0]["KetLuan"].ToString();
                if (skl.Trim().Equals("") || skl.Trim().Equals("NNull"))
                {
                    rptKQ.txtketluan.Text = "";
                    rptKQ.lblketluan.Visible = false;
                }
                else
                    rptKQ.txtketluan.Text = dt.Rows[0]["KetLuan"].ToString();

                rptKQ.txtDViChiDinh.Text = dt.Rows[0]["TenDViChiDinh"].ToString();
                rptKQ.txtnghe.Text = dt.Rows[0]["DienThoai"].ToString();
                rptKQ.txtchatBP.Text = Uit.it_Font.Vni2Unicode(dt.Rows[0]["ChatBP"].ToString(),false);

                if (Convert.ToBoolean(dt.Rows[0]["GioiTinh"].ToString()))
                    gioi = "Nam";
                else
                    gioi = "Nữ";

                rptKQ.barSoHS.Text = dt.Rows[0]["SoHS"].ToString();
                rptKQ.txtSoBN.Text = dt.Rows[0]["SoBN"].ToString();              
                rptKQ.txthoten.Text = dt.Rows[0]["HoTen"].ToString();
                rptKQ.txtGioiTinh.Text = gioi;
                rptKQ.txttuoi.Text = dt.Rows[0]["NamSinh"].ToString()
                                    + "(" + dt.Rows[0]["Tuoi"].ToString() + " Tuổi)";              
                rptKQ.txtchuandoan.Text = dt.Rows[0]["ChuanDoan"].ToString();
                DateTime ngaylm = new DateTime();
                ngaylm = Convert.ToDateTime(dt.Rows[0]["NgayLayMau"].ToString());
                rptKQ.txtngaylaymau.Text = ngaylm.ToString("dd/MM/yyyy");
                rptKQ.txtptv.Text = dt.Rows[0]["PTVien"].ToString();
                rptKQ.txtppmo.Text = Uit.it_Font.Vni2Unicode(dt.Rows[0]["PPMo"], false);

                cdate = DateTime.Parse(dt.Rows[0]["CDate"].ToString());
                rptKQ.txttendv.Text = "Đơn vị gửi mẫu:";
                rptKQ.txtketqualan.Text = "KẾT QUẢ";
                rptKQ.txtngaythang.Text = "Ngày " + cdate.Day.ToString("00") +
                                      " Tháng " + cdate.Month.ToString("00") +
                                      " Năm " + cdate.Year.ToString("0000");

                DateTime ngaynhan = new DateTime();
                ngaynhan = Convert.ToDateTime(dt.Rows[0]["NgayNhan"].ToString());
                rptKQ.txtngaynhanmau.Text = ngaynhan.ToString("dd/MM/yyyy, hh:mm");

                //PP nhuom
                string idppn = dt.Rows[0]["IDPPNhuom"].ToString();
                comPPNhuom.SetEditValue(idppn);
                rptKQ.txtPPNhuom.Text = comPPNhuom.Text;

                if (dt.Rows[0]["SlgMau"].ToString() != "")
                    rptKQ.txtSoMau.Text = dt.Rows[0]["SlgMau"].ToString();
                else
                    rptKQ.txtSoMau.Text = "1";
                rptKQ.txtSoLam.Text = dt.Rows[0]["SoLam"].ToString();
                rptKQ.txtNguoiCatLoc.Text = dt.Rows[0]["HoTenCat"].ToString();
                if (dt.Rows[0]["NgayCat"].ToString()!="")
                {
                    rptKQ.txtNgayCat.Text = Convert.ToDateTime(dt.Rows[0]["NgayCat"]).ToString("dd/MM/yyyy");
                }
                else
                    rptKQ.txtNgayCat.Text = "";
                rptKQ.txtNguoiLamTB.Text = dt.Rows[0]["HoTenKTV"].ToString();
                if (dt.Rows[0]["NgayLenLam"].ToString() != "")
                {
                    rptKQ.txtNgayLam.Text = Convert.ToDateTime(dt.Rows[0]["NgayLenLam"]).ToString("dd/MM/yyyy");
                }
                else
                    rptKQ.txtNgayLam.Text = "";
                //rptKQ.txtNgayLam.Text = Convert.ToDateTime(dt.Rows[0]["NgayLenLam"]).ToString("dd/MM/yyyy");
                
                url_1 = hensu.url + ngaynhan.Year + "\\"
                            + ngaynhan.Year.ToString().Substring(2, 2) + ngaynhan.Month.ToString("00")
                            + "\\x100_" + STB + ".jpg";

                url_2 = hensu.url + ngaynhan.Year + "\\"
                + ngaynhan.Year.ToString().Substring(2, 2) + ngaynhan.Month.ToString("00")
                + "\\x400_" + STB + ".jpg";

                if (File.Exists(url_1))
                    rptKQ.piclan1.Image = new Bitmap(url_1);
                else
                    rptKQ.piclan1.Visible = false;

                if (File.Exists(url_2))
                    rptKQ.piclan2.Image = new Bitmap(url_2);
                else
                    rptKQ.piclan2.Visible = false;

                if (rptKQ.piclan1.Image == null)
                {
                    rptKQ.piclan1.Visible = false;
                }
                if (rptKQ.piclan2.Image == null)
                {
                    rptKQ.piclan2.Visible = false;
                }
                rptKQ.txtngaygioin.Text = DateTime.Now.ToString("dd/MM/yyyy, hh:mm:ss");
                rptKQ.txtbsthuchien.Text = dt.Rows[0]["HoTenBSTH"].ToString();
                if (dt.Rows[0]["UserUpdate"].ToString() != "")
                    rptKQ.txtThuKy.Text = dt.Rows[0]["UserUpdate"].ToString();
                else
                    rptKQ.txtThuKy.Text = dt.Rows[0]["UserName"].ToString();
                if (dt.Rows[0]["SoLamHM"].ToString() != "" || dt.Rows[0]["SoLamHM"].ToString() != "0")
                    rptKQ.txtGhiChu.Text = dt.Rows[0]["SoLamHM"].ToString() + " tiêu bản HMMD";
                if (dt.Rows[0]["SlgMauCat"].ToString() != "")
                    rptKQ.txtSLgMauGui.Text = dt.Rows[0]["SlgMauCat"].ToString();
                string SNVien = dt.Rows[0]["SoNhapVien"].ToString();
                if (SNVien != "")
                {
                    string sql = ""
                                       + "SELECT                   \n"
                                       + "	sonhapvien              \n"
                                       + "	,traibenh               \n"
                                       + "	,dv.TenDonVi            \n"
                                       + "FROM nhapvien            \n"
                                       + "INNER JOIN his.dbo.utb_DM_DonVi AS dv ON traibenh = dv.MaDonVi\n"
                                       + "WHERE sonhapvien = '" + SNVien + "'\n";
                    DataTable dttraiBenh = Uit.it_SQL.getDataTable(sql, conpk);
                    if (dttraiBenh != null)
                    {
                        rptKQ.txtTraiBenh.Text = dttraiBenh.Rows[0]["TenDonVi"].ToString() + " ";
                    }
                    else
                    {
                        rptKQ.txtTraiBenh.Text = "";
                    }
                }
            }
            else
            {
                return 1;
            }           
            
            return 0;
        }

        public int SerDataToReport_FNA(rptKQFNA rptKQ, string STB, int lan)
        {
            DateTime cdate = DateTime.Today;
            DataTable dt = KQBUS.GetKetQua(STB, lan);
            string url_1 = "";
            string url_2 = "";
            string gioi = "";

            if (dt.Rows.Count > 0)
            {

                rptKQ.txtSoTieuBan.Text = dt.Rows[0]["SoTieuBan"].ToString();
                rptKQ.txtDiaChi.Text = dt.Rows[0]["DiaChi"].ToString();
                string dait, kl;
                dait = dt.Rows[0]["KQDaiThe"].ToString();
                if (dait == "NNull")
                    dait = "";

                rptKQ.txtdaithe.Text = dait;
                rptKQ.txtvithe.Text = dt.Rows[0]["KQViThe"].ToString();
                string skl = dt.Rows[0]["KetLuan"].ToString();
                if (skl.Trim().Equals("") || skl.Trim().Equals("NNull"))
                {
                    rptKQ.txtketluan.Text = "";
                    rptKQ.lblketLuan.Visible = false;
                }
                else
                    rptKQ.txtketluan.Text = dt.Rows[0]["KetLuan"].ToString();

                rptKQ.txtDViChiDinh.Text = dt.Rows[0]["TenDViChiDinh"].ToString();
                rptKQ.txtnghe.Text = dt.Rows[0]["DienThoai"].ToString();
                rptKQ.txtchatbenhpham.Text = dt.Rows[0]["KQDaiThe"].ToString();

                if (Convert.ToBoolean(dt.Rows[0]["GioiTinh"].ToString()))
                    gioi = "Nam";
                else
                    gioi = "Nữ";

                rptKQ.barSoHS.Text = dt.Rows[0]["SoHS"].ToString();
                rptKQ.txtSoBN.Text = dt.Rows[0]["SoBN"].ToString();
                rptKQ.txthoten.Text = dt.Rows[0]["HoTen"].ToString();
                rptKQ.txtGioiTinh.Text = gioi;
                rptKQ.txttuoi.Text = dt.Rows[0]["NamSinh"].ToString()
                                    + "(" + dt.Rows[0]["Tuoi"].ToString() + " Tuổi)";
                rptKQ.txtchuandoan.Text = dt.Rows[0]["ChuanDoan"].ToString();
                DateTime ngaylm = new DateTime();
                ngaylm = Convert.ToDateTime(dt.Rows[0]["NgayLayMau"].ToString());
                rptKQ.txtngaylaymau.Text = ngaylm.ToString("dd/MM/yyyy");
                rptKQ.txtptv.Text = dt.Rows[0]["PTVien"].ToString();
                rptKQ.txtbscd.Text = dt.Rows[0]["HoTenBSCD"].ToString();

                cdate = DateTime.Parse(dt.Rows[0]["CDate"].ToString());
                //rptKQ.txttendv.Text = "Đơn Vị:";
                rptKQ.txtketqualan.Text = "KẾT QUẢ";
                rptKQ.txtngaythang.Text = "Ngày " + cdate.Day.ToString("00") +
                                      " Tháng " + cdate.Month.ToString("00") +
                                      " Năm " + cdate.Year.ToString("0000");

                DateTime ngaynhan = new DateTime();
                ngaynhan = Convert.ToDateTime(dt.Rows[0]["NgayNhan"].ToString());
                rptKQ.txtngaynhanmau.Text = ngaynhan.ToString("dd/MM/yyyy, hh:mm");

                url_1 = hensu.url + ngaynhan.Year + "\\"
                            + ngaynhan.Year.ToString().Substring(2, 2) + ngaynhan.Month.ToString("00")
                            + "\\x100_" + STB + ".jpg";

                url_2 = hensu.url + ngaynhan.Year + "\\"
                + ngaynhan.Year.ToString().Substring(2, 2) + ngaynhan.Month.ToString("00")
                + "\\x400_" + STB + ".jpg";

                if (File.Exists(url_1))
                    rptKQ.piclan1.Image = new Bitmap(url_1);
                else
                    rptKQ.piclan1.Visible = false;

                if (File.Exists(url_2))
                    rptKQ.piclan2.Image = new Bitmap(url_2);
                else
                    rptKQ.piclan2.Visible = false;

                if (rptKQ.piclan1.Image == null)
                {
                    rptKQ.piclan1.Visible = false;
                }
                if (rptKQ.piclan2.Image == null)
                {
                    rptKQ.piclan2.Visible = false;
                }
                rptKQ.txtngaygioin.Text = DateTime.Now.ToString("dd/MM/yyyy, hh:mm:ss");
                rptKQ.txtbsthuchien.Text = dt.Rows[0]["HoTenBSTH"].ToString();
              
            }
            else
            {
                return 1;
            }

            return 0;
        }

        public int SerDataToReport_PAP(rptKQPAP rptKQ, string STB)
        {
            DateTime cdate = DateTime.Today, cdate2 = DateTime.Today;
            DataTable dt = KQBUS.GetKetQuaPAP_STB(STB);
            string url_1 = "";
            string url_2 = "";
            string gioi = "";

            if (dt.Rows.Count > 0)
            {
                rptKQ.txtSoTieuBan.Text = dt.Rows[0]["SoTieuBan"].ToString().ToUpper();
                rptKQ.txtDiaChi.Text = dt.Rows[0]["DiaChi"].ToString();
                rptKQ.txtketluan.Text = dt.Rows[0]["KetLuan"].ToString();

                rptKQ.txtbslaymau.Text = dt.Rows[0]["PTVien"].ToString();
                rptKQ.txtDViChiDinh.Text = dt.Rows[0]["TenDViChiDinh"].ToString();
                
                if (Convert.ToBoolean(dt.Rows[0]["GioiTinh"].ToString()))
                    gioi = "Nam";
                else
                    gioi = "Nữ";

                rptKQ.barSoHS.Text = dt.Rows[0]["SoHS"].ToString();
                rptKQ.txtSoBN.Text = dt.Rows[0]["SoBN"].ToString();
                rptKQ.txthoten.Text = dt.Rows[0]["HoTen"].ToString().ToUpper();
                rptKQ.txtGioiTinh.Text = gioi;
                rptKQ.txttuoi.Text = dt.Rows[0]["NamSinh"].ToString()
                                    + "(" + dt.Rows[0]["Tuoi"].ToString() + " Tuổi)";
                rptKQ.txtbsthuchien.Text = dt.Rows[0]["HoTenBSTH"].ToString();

                rptKQ.txtchuandoan.Text = dt.Rows[0]["ChuanDoan"].ToString();

                DateTime ngaylm = new DateTime();
                ngaylm = Convert.ToDateTime(dt.Rows[0]["NgayLayMau"].ToString());
                rptKQ.txtngaylaymau.Text = ngaylm.ToString("dd/MM/yyyy");

                DateTime ngaynhan = new DateTime();
                ngaynhan = Convert.ToDateTime(dt.Rows[0]["NgayNhan"].ToString());
                rptKQ.txtngaynhanmau.Text = ngaynhan.ToString("dd/MM/yyyy, hh:mm");
                                          
                cdate = DateTime.Parse(dt.Rows[0]["CDate"].ToString());
                rptKQ.txttendv.Text = "Đơn Vị:";

                rptKQ.txtngaythang.Text = "Ngày " + cdate.Day.ToString("00") +
                                      " Tháng " + cdate.Month.ToString("00") +
                                      " Năm " + cdate.Year.ToString("0000");

                if (dt.Rows[0]["KetLuan"].ToString() == "")
                {
                    return 1;
                }

                url_1 = hensu.url + ngaynhan.Year + "\\"
                          + ngaynhan.Year.ToString().Substring(2, 2) + ngaynhan.Month.ToString("00")
                          + "\\x100_" + STB + ".jpg";

                url_2 = hensu.url + ngaynhan.Year + "\\"
                + ngaynhan.Year.ToString().Substring(2, 2) + ngaynhan.Month.ToString("00")
                + "\\x400_" + STB + ".jpg";

                if (File.Exists(url_1))
                    rptKQ.piclan1.Image = new Bitmap(url_1);
                else
                    rptKQ.piclan1.Visible = false;

                if (File.Exists(url_2))
                    rptKQ.piclan2.Image = new Bitmap(url_2);
                else
                    rptKQ.piclan2.Visible = false;

                rptKQ.txtngaygioin.Text = DateTime.Now.ToString("dd/MM/yyyy, hh:mm:ss");
             
                checkKQ(rptKQ, dt);
            }
            else
            {
                return 1;
            }
           
            return 0;
        }

        private void checkKQ(rptKQPAP rptKQ, DataTable dt)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                int loaiBP = int.Parse(dt.Rows[i]["LoaiBenhPham"].ToString());
                int IDNhomBenh = int.Parse(dt.Rows[i]["IDNhomBenh"].ToString());
                int IDBenh = int.Parse(dt.Rows[i]["IDBenh"].ToString());
                if (loaiBP == 1)
                {
                    rptKQ.chknhungdich.Checked = true;
                }
                else
                {
                    rptKQ.chkthuongquy.Checked = true;
                }
                switch (IDNhomBenh)
                {
                    case 1:
                        switch (IDBenh)
                        {
                            case 2:
                                rptKQ.txt12.Checked = true;
                                break;
                            case 3:
                                rptKQ.txt13.Checked = true;
                                break;
                            case 4:
                                rptKQ.txt14.Checked = true;
                                break;
                            case 5:
                                rptKQ.txt15.Checked = true;
                                break;
                            case 6:
                                rptKQ.txt16.Checked = true;
                                break;
                            case 30:
                                rptKQ.txt30.Checked = true;
                                break;
                        }
                        break;
                    case 2:
                        switch (IDBenh)
                        {
                            case 7:
                                rptKQ.txt27.Checked = true;
                                break;
                            case 10:
                                rptKQ.txt210.Checked = true;
                                break;
                            case 11:
                                rptKQ.txt211.Checked = true;
                                break;
                            case 12:
                                rptKQ.txt212.Checked = true;
                                break;
                            case 13:
                                rptKQ.txt213.Checked = true;
                                break;
                            case 14:
                                rptKQ.txt214.Checked = true;
                                break;
                        }
                        break;
                    case 3:
                        switch (IDBenh)
                        {
                            case 20:
                                rptKQ.txt320.Checked = true;
                                break;
                            //case 21:
                            //    rptKQ.txt321.Checked = true;
                            //    break;
                            //case 22:
                            //    rptKQ.txt322.Checked = true;
                            //    break;
                            //case 23:
                            //    rptKQ.txt323.Checked = true;
                            //    break;
                            //case 24:
                            //    rptKQ.txt324.Checked = true;
                            //    break;
                            case 25:
                                rptKQ.txt325.Checked = true;
                                break;
                            case 26:
                                rptKQ.txt326.Checked = true;
                                break;
                            case 27:
                                rptKQ.txt327.Checked = true;
                                break;
                            case 28:
                                rptKQ.txt328.Checked = true;
                                break;
                            case 29:
                                rptKQ.txt329.Checked = true;
                                break;
                        }
                        break;
                    case 4:
                        switch (IDBenh)
                        {
                            case 15:
                                rptKQ.txt415.Checked = true;
                                break;
                            case 16:
                                rptKQ.txt416.Checked = true;
                                break;
                            case 17:
                                rptKQ.txt417.Checked = true;
                                break;
                            case 18:
                                rptKQ.txt418.Checked = true;
                                break;
                            case 19:
                                rptKQ.txt419.Checked = true;
                                break;
                            case 31:
                                rptKQ.txt31.Checked = true;
                                break;
                            case 32:
                                rptKQ.txt420.Checked = true;
                                break;
                        }
                        break;

                }
            }
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.conpk.Dispose();
            this.Close();            
        }

        private void txtSoTieuBanTu_Validating(object sender, CancelEventArgs e)
        {
            int ou = 0;
            if (!int.TryParse(txtSoTieuBanTu.Text, out ou))
            {
                MessageBox.Show("Số thứ tự tiêu bản phải là số nguyên!", "Thông báo");
                
                txtSoTieuBanTu.Focus();
                txtSoTieuBanTu.Select();
            }
        }

        private void txtSoTieuBanDen_Validating(object sender, CancelEventArgs e)
        {
            int ou = 0;
            if (!int.TryParse(txtSoTieuBanDen.Text, out ou))
            {
                MessageBox.Show("Số thứ tự tiêu bản phải là số nguyên!", "Thông báo");

                txtSoTieuBanDen.Focus();
                txtSoTieuBanDen.Select();
            }
        }
    }
}